package hamsterpolice.todo.data;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class CategoryTable {
	
	/**
	 * Hidden constructor. Only static methods on this class.
	 */
	private CategoryTable(){}
	
	private static final String TAG = "CategoryTable";
	public static final String TABLE_NAME = "category";
	
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_NAME = "name";
	public static final String COLUMN_USER_ID = "user_id";
	
	private static final String TABLE_CREATE = 
			" CREATE TABLE " + TABLE_NAME + 
			" ( " + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ COLUMN_NAME + " TEXT COLLATE NOCASE NOT NULL, "
			+ COLUMN_USER_ID + " INTEGER NOT NULL, "
			+ "FOREIGN KEY(" + COLUMN_USER_ID + ") REFERENCES " + UserTable.TABLE_NAME + "(" + UserTable.COLUMN_ID + ") ON UPDATE CASCADE ON DELETE RESTRICT ); "
			+ "CREATE UNIQUE INDEX IDX_USER_ID_NAME ON " + TABLE_NAME + "(" + COLUMN_USER_ID + ", " + COLUMN_NAME + ");";
	
	/**
	 * Creates the user table
	 * @param db
	 */
	static void create(SQLiteDatabase db) {
		Log.i(TAG, "Creating table " + TABLE_NAME);
		db.execSQL(TABLE_CREATE);
	}

	/**
	 * Upgrades the user table
	 * @param db
	 * @param oldVersion
	 * @param newVersion
	 */
	static void upgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if(oldVersion<6){
			db.execSQL("DROP TABLE " + TABLE_NAME);
			create(db);
		}
	}
	
}
